package pushtest.mobike.com.mtpush.ui

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.graphics.Color
import android.os.Bundle
import android.support.v4.widget.NestedScrollView
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import kotlinx.android.synthetic.main.activity_scrolling2.*
import kotlinx.android.synthetic.main.content_scrolling2.*
import pushtest.mobike.com.mtpush.R
import pushtest.mobike.com.mtpush.ui.adapter.MyMultiItem
import pushtest.mobike.com.mtpush.ui.adapter.ScrollingSectionAdapter
import pushtest.mobike.com.mtpush.ui.behavior.EasyBehavior
import pushtest.mobike.com.mtpush.ui.behavior.LocationBehavior
import pushtest.mobike.com.mtpush.ui.behavior.MyTestBehaivor
import pushtest.mobike.com.mtpush.utils.dp2px
import pushtest.mobike.com.mtpush.utils.snackbar
import pushtest.mobike.com.mtpush.utils.toast
import timber.log.Timber

class ScrollingActivity2 : AppCompatActivity() {
  lateinit var adapter: ScrollingSectionAdapter
  lateinit var header: View
  lateinit var easyBehavior: EasyBehavior<NestedScrollView>

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_scrolling2)
    setSupportActionBar(toolbar)
    fab.setOnClickListener { view ->
      adapter.addData(MyMultiItem(MyMultiItem.unlock, "这是开锁"))
      if (adapter.data.size > 8) {

        easyBehavior.setPeekHeight(view.context.dp2px(100));
      }
    }
    location.setOnClickListener {
      easyBehavior.state = EasyBehavior.STATE_ADD_TOP_VIEW
      if(layout_top .visibility != View.VISIBLE) {
        tip.translationY = this@ScrollingActivity2.dp2px(60).toFloat()
        tip.text = "这是 动画 tip View"
        tip.animate()
            .translationY(0F)
            .alpha(1F)
            .setListener(
                object : AnimatorListenerAdapter() {
                  override fun onAnimationEnd(animation: Animator?) {
                    super.onAnimationEnd(animation)
                    layout_top.visibility = View.VISIBLE
                  }

                }
            )

            .start()
      }else{
        top1.visibility =View.VISIBLE
      }
    }
    initRecyclerView()
  }

  private fun initRecyclerView() {
    setupBehavior()
    initRecyclerView(setupAdapter(), null)

  }

  private fun setupAdapter(): ScrollingSectionAdapter {
    val lists = mutableListOf<MyMultiItem>()
    for (i in 1..20) {
      when (i % 3) {
        0 -> lists.add(MyMultiItem(MyMultiItem.tip, "这是tip $i"))
        1 -> lists.add(MyMultiItem(MyMultiItem.unlock, "这是开锁"))
        2 -> lists.add(
            MyMultiItem(
                MyMultiItem.ad,
                "https://cache.bestmvno.com/wp-content/uploads/2016/02/Verizon-FreeBee-Data-1-1080x280.jpg"
            )
        )
      }

    }
//    val adapter = ScrollingViewAdapter(android.R.layout.simple_list_item_1, lists)
    adapter = ScrollingSectionAdapter(lists,
        object : AnimatorListenerAdapter() {
          override fun onAnimationStart(animation: Animator?) {
            LocationBehavior.from(location).setState(LocationBehavior.FIRST)
//          location.animate().translationY(this@ScrollingActivity.dp2px(-60).toFloat()).start()
          }
        })


    adapter.setOnItemClickListener { adapter, view, position ->
      view.context.toast("setOnItemChildClickListener $position")
    }
    adapter.setOnItemChildClickListener { adapter, view, position ->
      when (view.id) {
        R.id.unlcok ->
          view.snackbar("开锁")
        R.id.report ->
          view.snackbar("上报故障")
        R.id.service ->
          view.snackbar("客服服务")
      }
    }
    adapter.setNotDoAnimationCount(3)//设置不显示动画数量
//    adapter.setPreLoadNumber(2)
    adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT)
    return adapter
  }


  private fun initRecyclerView(
      adapter: ScrollingSectionAdapter,
      behavior: MyTestBehaivor<NestedScrollView>?
  ) {
    recycler.isNestedScrollingEnabled = false
    recycler.setHasFixedSize(false)
    val layoutManager = LinearLayoutManager(this)
    layoutManager.isAutoMeasureEnabled = true
    recycler.layoutManager = layoutManager
    recycler.adapter = adapter
    recycler.scrollToPosition(0)
    recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
      override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
        Timber.i("dx = $dx  dy = $dy")
      }

      override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        Timber.i(" new State = $newState")
      }
    })
//    design_bottom_sheet.postDelayed({
//      behavior?.peekHeight = design_bottom_sheet.context.dp2px(500)
//    }, 1000)
  }

  private fun setupBehavior(): EasyBehavior<NestedScrollView>? {
    easyBehavior = EasyBehavior.from(design_bottom_sheet)
//    behavior.peekHeight = this@ScrollingActivity.dp2px(300)
    easyBehavior.callback = object : EasyBehavior.BottomSheetCallback {
      override fun onSlide(view: View, p1: Float) {
        Timber.i(
            "onSlide  $p1"
        )
        if (view.top < this@ScrollingActivity2.dp2px(100)) {
          root_view.setBackgroundColor(Color.parseColor("#88000000"))
        } else {
          root_view.setBackgroundColor(Color.TRANSPARENT)
        }
        if (p1 == 1F) {
          Timber.i("height = ${view.height}   rawY = ${view.y}   ${view.top}")
        }

      }

      override fun onStateChanged(p0: View, p1: Int) {
        Timber.i(" p1 =  $p1")

      }

    }
    return easyBehavior
  }
}
